package {
	import flash.display.*;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.Video;
	import flash.net.URLRequest;
	import flash.utils.setInterval;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import gs.TweenLite;
	
	import mx.core.MovieClipLoaderAsset;

	public class Pong extends Sprite {
		private var detector:Detector;
		private var camera:Video;
		[Embed(source="../art/Field_NoShadow.swf")] 
		private var FieldClass:Class;
		private var field:MovieClipLoaderAsset;
		[Embed(source="../art/pelota.swf")] 
		private var BallClass:Class;
		private var ball:MovieClipLoaderAsset;
		private var ballWrapper:Rectangle;
		[Embed(source="../art/Player1.swf")] 
		private var Player1Class:Class;
		private var player1:MovieClipLoaderAsset;
		private var myWidth:int = 480; 
		private var myHeight:int = 360; 
		private var fieldPercentage:Number = 0.2;
		// Ball movement
		public var velX:Number = 10;
		public var velY:Number = 10;
		public var frames:int = 0;
		private var startSide:Number;
		public var musicChannel:SoundChannel;
		private var isFirstRun:Boolean = true;

		
		public function Pong() {
			
			this.detector = new PaletaPosition();
			this.camera = detector.getVideo();
			this.camera.x = 0;
			this.camera.y = 0;
			this.camera.width = this.myWidth;
			this.camera.height = this.myHeight;
			this.addChild( this.camera );
			
			this.field = new FieldClass();
			this.field.x = this.height * this.fieldPercentage / 2;
			this.field.y = this.height * this.fieldPercentage / 2;
			this.field.width = this.myWidth - this.height * this.fieldPercentage;
			this.field.height = this.myHeight - this.height * this.fieldPercentage;
			this.addChild( this.field );
			
			this.ball = new BallClass();
			this.ball.x = this.width/2;
			this.ball.y = this.height/2;
			this.addChild( this.ball );
			this.ballWrapper = new Rectangle( );
			this.ballWrapper.width = 15;
			this.ballWrapper.height = 15;
			this.ballWrapper.x = this.ball.x;
			this.ballWrapper.y = this.ball.y;
			
			this.player1 = new Player1Class();
			this.player1.x = this.field.x;
			this.player1.y = 0;
			this.addChild( this.player1 );
			this.PlayCrowd();
			setInterval(gameLoop, 25);

		}
		
		public function gameLoop():void {
			var aux:Number;
			var duration:int;
			
			if(isFirstRun){
				this.PlayWhistle();
				isFirstRun = false;
			}
			
			/* Aca con un 1 anda bien, sino el tipo se mueve medio trabado */
			if( this.frames % 2 == 0 ) {
				this.detector.detect();
				aux = (detector.getY() - this.player1.height / 2) * this.myHeight / 480;
				//duration = Math.abs(this.player1.y - aux) * 0.1;
				duration = 1;
				TweenLite.to(this.player1, duration, {y:aux});
//				this.player1.x = detector.getX().valueOf() * this.myWidth / 640 + this.correctionX;
//				this.player1.y = detector.getY().valueOf() * this.myHeight / 480;
			}
			
			if ( this.frames == 0 ){
				
				//El juego va a arrancar - Hago un random a ver hacia que lado.
				this.ball.x = this.width/2;
				this.ball.y = this.height/2;
				
				startSide = Math.round(Math.random());
				if(startSide < 1){
					// Arranco a la izquierda
					velX = -10;
				} else {
					velX = 10;
				}
				
				startSide = Math.round(Math.random());
				if(startSide < 1){
					// Arranco hacia abajo
					velY = 10;
				}else{
					velY = -10;
				}
				
			}
			
//			var startTime:int = getTimer();
//			while (true) {
//				if (getTimer() - startTime >= 8000) 
//					break;
//			}
			updateBall();
			isGoal();
			
			this.frames++;
		}
		
		public function updateBall():void {
			if( this.player1.x < this.ball.x && this.player1.x + this.player1.width > this.ball.x ) {
				if( this.player1.y < this.ball.y && this.player1.y + this.player1.height > this.ball.y ) {
					this.velX = -this.velX;
					if( this.ball.y < this.player1.y + this.player1.height / 4 || this.ball.y > this.player1.y + this.player1.height - this.player1.height / 4){ 
						this.velY = this.velY * 1.25;
					}else{
						this.velY = this.velY * 0.75;
					}
					PlayKick();
				}else if( this.player1.y < this.ball.y + this.velY && this.player1.y + this.player1.height > this.ball.y + this.velY ) {
					this.velY = -this.velY;
					PlayKick();
				} 
			}
			if( ( this.ball.x >= this.myWidth - this.field.x) || ( this.ball.x <= this.field.x ) ) {
				velX = -velX;
			}
			if( ( this.ball.y >= this.myHeight - this.field.y) || ( this.ball.y <= this.field.y ) ) {
				velY = -velY;
			}
			this.ball.x = this.ball.x + this.velX;
			this.ballWrapper.x = this.ball.x;
			this.ball.y = this.ball.y + this.velY;
			this.ballWrapper.y = this.ball.y;
		}
		
		public function isGoal():int {
			if( this.ball.x <= 65 && this.ball.y >= 135 && this.ball.y <= 225 && this.velX < 0){
				this.frames = -1;
				isFirstRun = true;
				this.PlayGoal();
				return 1;
			}else if( this.ball.x >= 415 && this.ball.y >= 135 && this.ball.y <= 225 && this.velX > 0){
				this.frames = -1;
				isFirstRun = true;
				this.PlayGoal();
				return 2;
			}else{
				return 0;
			}
		}
		
		public function PlayKick():void {
			var Kick:Sound = new Sound(new URLRequest("../sounds/Kick.mp3"));
			Kick.play();
		}
		
		public function PlayCrowd():void {
			var Crowd:Sound = new Sound(new URLRequest("../sounds/Crowd.mp3"));
			musicChannel = Crowd.play();
			musicChannel.addEventListener(Event.SOUND_COMPLETE, loopMusic);
		}
	
		public function loopMusic(e:Event):void{
			if (musicChannel != null) {
				musicChannel.removeEventListener(Event.SOUND_COMPLETE, loopMusic);
				PlayCrowd();
			}
		}
		
		public function PlayWhistle():void {
			var Whistle:Sound = new Sound(new URLRequest("../sounds/WHISTLE.mp3"));
			Whistle.play();
		}
		
		public function PlayGoal():void {
			var Goal:Sound = new Sound(new URLRequest("../sounds/GOL.mp3"));
			Goal.play();
		}
	}
}
